package com.example.demo.utils.SpiderUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;


public class Spider {

    public static WebContent fetchTitleAndImages(String url) {
        try {
            Document doc = Jsoup.connect(url).get();
            if(url.contains("bilibili")){
                return new WebContent("B站","https://www.baijingapp.com/uploads/product/16511192803960/165111928035679.png");
            }else if(url.contains("csdn")){
                return new WebContent("CSDN","https://g.csdnimg.cn/static/logo/favicon32.ico");
            }else{
                Element icon = doc.selectFirst("link[rel~=(shortcut|icon)]");
                String faviconUrl = icon!=null? icon.absUrl("href"):"https://www.baidu.com/favicon.ico";
                return new WebContent("CSDN",faviconUrl);//获取favicon
            }
        } catch (IOException e) {
            //e.printStackTrace();
            System.out.println("爬虫失败了！！！！！");
            if(url.contains("bilibili")){
                return new WebContent("B站","https://www.baijingapp.com/uploads/product/16511192803960/165111928035679.png");
            }else if(url.contains("csdn")){
                return new WebContent("CSDN","https://g.csdnimg.cn/static/logo/favicon32.ico");
            }else{
                return new WebContent("失败了","https://www.baidu.com/favicon.ico"); // 或者抛出异常，根据具体需求
            }
        }
    }
    public static String fetchInfo(String url){
        String result="";
        try {
            String cleanUrl=url.substring(0,url.indexOf("%"));
            Document doc = Jsoup.connect(cleanUrl).get();

            Elements paragraphs = doc.select("p");
                        for (Element paragraph : paragraphs) {
                            // 打印所有段落的内容
                            result+= paragraph.text();
                            }
        }catch(IOException e){
            e.printStackTrace();
        }
        if(result.length()>700)
        return result.substring(0,700);
        else{
            return result;
        }
    }

}
